package com.lingnan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.lingnan.mapper.TargetMapper;
import com.lingnan.pojo.Active;
import com.lingnan.mapper.ActiveMapper;
import com.lingnan.pojo.Target;
import com.lingnan.service.ActiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingnan.util.TencentCosUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * 活动表 服务实现类
 * </p>
 *
 * @author 梁兴达
 * @since 2022-03-06
 */
@Service
public class ActiveServiceImpl extends ServiceImpl<ActiveMapper, Active> implements ActiveService {

    @Autowired
    private ActiveMapper activeMapper;
    @Autowired
    private TargetMapper targetMapper;

    @Autowired
    private TencentCosUtils tencentCosUtils;

    @Override
    public List<Active> searchActive(String name) {
        QueryWrapper<Active> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("competition_type", "校内", "校外", "非比赛")
                .eq("info_type", "非通知")
                .like("name",name)
                .orderByDesc("begin_time", "end_time");
        List<Active>  list = activeMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<Active> searchNotice(String name) {
        QueryWrapper<Active> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("competition_type", "通知")
                .orderByDesc("begin_time", "end_time")
                .like("name",name);
        List<Active>  list = activeMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public boolean publishActive(Active active) {
        try{
            //对文件存入cos处理
            String fileName = tencentCosUtils.upload(active.getFileUrl());
            //存入后获取文件全链接存入数据库
            String url = tencentCosUtils.getUrl(fileName).toString();
            active.setFileUrl(url);
            activeMapper.insert(active);
            ConcurrentHashMap<String, Object> objectObjectConcurrentHashMap = new ConcurrentHashMap<>();
            objectObjectConcurrentHashMap.put("id", active.getId());
            List<Active> actives = activeMapper.selectByMap(objectObjectConcurrentHashMap);
            Target target = new Target();
            target.setActiveId(actives.get(0).getId());
            target.setSchoolId(actives.get(0).getSchoolId());
            target.setGrade(actives.get(0).getGrade());
            target.setMajorId(actives.get(0).getMajorId());
            if (!ObjectUtils.isEmpty(actives) && targetMapper.insert(target)>0) {
                return true;
            }
            return false;
        }catch (Exception e){
            return false;
        }
    }
//        if (activeMapper.insert(active) > 0) {
//            ConcurrentHashMap map = new ConcurrentHashMap(1);
//            map.put("name", active.getName());
//            List list = activeMapper.selectByMap(map);
//            Active active1 = (Active) list.get(0);
//            Target target = new Target();
//            target.setActiveId(active1.getId());
//            if (targetMapper.insert(target) > 0) {
//                return true;
//            }
//        }
//    return false;
//
//}
    @Override
    public boolean updateActive(Active active) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",active.getId());
        activeMapper.update(active,queryWrapper);
        ConcurrentHashMap<String, Object> objectObjectConcurrentHashMap = new ConcurrentHashMap<>();
        objectObjectConcurrentHashMap.put("name", active.getName());
        List<Active> actives = activeMapper.selectByMap(objectObjectConcurrentHashMap);
        //增加发布对象
        Target target = new Target();
        target.setActiveId(actives.get(0).getId());
        target.setSchoolId(actives.get(0).getSchoolId());
        target.setGrade(actives.get(0).getGrade());
        target.setMajorId(actives.get(0).getMajorId());
        if (!ObjectUtils.isEmpty(actives) && targetMapper.insert(target)>0) {
            return true;
        }
        return false;
    }

    @Override
    public Integer recoveryById(Integer id) {
        return activeMapper.recoveryById(id);
    }


}